package Utils;

import java.util.Hashtable;

public class NCRDecimal2Unicode {
	private static Hashtable<String, String> hash;

	public static final String Unicode[] = { "\u00e1", "\u00e0", "\u1ea3", "\u00e3",
			"\u1ea1", // a
			"\u00c1", "\u00c0", "\u1ea2", "\u00c3", "\u1ea0", // A
			"\u00e2", "\u1ea5", "\u1ea7", "\u1ea9", "\u1eab", "\u1ead", // a^
			"\u00c2", "\u1ea4", "\u1ea6", "\u1ea8", "\u1eaa", "\u1eac", // A^
			"\u0103", "\u1eaf", "\u1eb1", "\u1eb3", "\u1eb5", "\u1eb7", // a(
			"\u0102", "\u1eae", "\u1eb0", "\u1eb2", "\u1eb4", "\u1eb6", // A(
			"\u0111", // dd
			"\u0110", // DD
			"\u00e9", "\u00e8", "\u1ebb", "\u1ebd", "\u1eb9", // e
			"\u00c9", "\u00c8", "\u1eba", "\u1ebc", "\u1eb8", // E
			"\u00ea", "\u1ebf", "\u1ec1", "\u1ec3", "\u1ec5", "\u1ec7", // e^
			"\u00ca", "\u1ebe", "\u1ec0", "\u1ec2", "\u1ec4", "\u1ec6", // E^
			"\u00ed", "\u00ec", "\u1ec9", "\u0129", "\u1ecb", // i
			"\u00cd", "\u00cc", "\u1ec8", "\u0128", "\u1eca", // I
			"\u00f3", "\u00f2", "\u1ecf", "\u00f5", "\u1ecd", // o
			"\u00d3", "\u00d2", "\u1ece", "\u00d5", "\u1ecc",// O
			"\u00f4", "\u1ed1", "\u1ed3", "\u1ed5", "\u1ed7", "\u1ed9", // o^
			"\u00d4", "\u1ed0", "\u1ed2", "\u1ed4", "\u1ed6", "\u1ed8", // O^
			"\u01a1", "\u1edb", "\u1edd", "\u1edf", "\u1ee1", "\u1ee3", // o+
			"\u01a0", "\u1eda", "\u1edc", "\u1ede", "\u1ee0", "\u1ee2", // O+
			"\u00fa", "\u00f9", "\u1ee7", "\u0169", "\u1ee5", // u
			"\u00da", "\u00d9", "\u1ee6", "\u0168", "\u1ee4", // U
			"\u01b0", "\u1ee9", "\u1eeb", "\u1eed", "\u1eef", "\u1ef1", // u+
			"\u01af", "\u1ee8", "\u1eea", "\u1eec", "\u1eee", "\u1ef0", // U+
			"\u00fd", "\u1ef3", "\u1ef7", "\u1ef9", "\u1ef5",// y
			"\u00dd", "\u1ef2", "\u1ef6", "\u1ef8", "\u1ef4"

	};

	public static final String NCRDecimal[] = {
		// NCR Decimal
			"&#225;", "&#224;", "&#7843;", "&#227;", "&#7841;",// a
			"&#193;", "&#192;", "&#7842;", "&#195;","&#7840;",
			 "&#226;", "&#7845;", "&#7847;", "&#7849;", "&#7851;", "&#7853;",// a^
			 "&#194;","&#7844;", "&#7846;", "&#7848;", "&#7850;" ,"&#7852;",
			 "&#259;", "&#7855;", "&#7857;", "&#7859;", "&#7861;", "&#7863;",// a(
			 "&#258;", "&#7854;", "&#7856;", "&#7858;", "&#7860;", "&#7862;",
			 "&#273;",//dd
			 "&#272;",
			 "&#233;", "&#232;", "&#7867;", "&#7869;", "&#7865;",//e
			 "&#201;", "&#200;", "&#7866;","&#7868;", "&#7864;",
			 "&#234;",	"&#7871;", "&#7873;", "&#7875;", "&#7877;", "&#7879;",// e^
			 "&#202;", "&#7870;","&#7872;","&#7874;","&#7876;", "&#7878;",
			"&#237;",	"&#236;", "&#7881;", "&#297;", "&#7883;", // i
			"&#205;", "&#204;", "&#7880;", "&#296;", "&#7882;",
			"&#243;", "&#242;",	"&#7887;", "&#245;", "&#7885;", // o
			"&#211;", "&#210;", "&#7886;","&#213;", "&#7884;",
			"&#244;", "&#7889;", "&#7891;",	"&#7893;", "&#7895;", "&#7897;",// o^
			"&#212;", "&#7888;", "&#7890;", "&#7892;", "&#7894;", "&#7896;",
			 "&#417;", "&#7899;", "&#7901;","&#7903;", "&#7905;", "&#7907;",//o+
			 "&#416;", "&#7898;", "&#7900;", "&#7902;", "&#7904;", "&#7906;",
			 "&#250;", "&#249;", "&#7911;",	"&#361;", "&#7909;",//u
			 "&#218;", "&#217;", "&#7910;", "&#360;", "&#7908;",
			"&#432;", "&#7913;", "&#7915;", "&#7917;","&#7919;", "&#7921;",//u+
			"&#431;", "&#7912;","&#7914;", "&#7916;", "&#7918;", "&#7920;",
			"&#253;", "&#7923;", "&#7927;", "&#7929;",	"&#7925;", //y
			"&#221;", "&#7922;", "&#7926;", "&#7928;", "&#7924;"
		};
	static {
		hash = new Hashtable<String, String>();
		for (int i = 0; i < NCRDecimal.length; i++) {
			hash.put(NCRDecimal[i],Unicode[i]);
		}
	}

	public static String toUnicode(String s) {
		// System.out.println(s);
		StringBuffer sb = new StringBuffer();
		int index1, index2 = 0;
		int fromIndex = 0;
		do {
			index1 = s.indexOf("&#", fromIndex);
			if (index1 >= 0) {
				index2 = s.indexOf(";", index1);
				if (index2 < 0) {
					break;
				}
				// System.out.println(index1 +"\t" + index2);
				String one_char1 = s.substring(index1, index2 + 1);
				// System.out.println(one_char1);
				String one_char2 = toUnicode1Char(one_char1);
				// System.out.println(one_char1+"---->"+one_char2);
				sb.append(s.substring(fromIndex, index1));
				sb.append(one_char2);
				fromIndex = index2 + 1;
			} else {
				break;
			}
		} while (true);
		sb.append(s.substring(fromIndex, s.length()));
		return new String(sb);
	}

	private static String toUnicode1Char(String l) {
		String re = hash.get(l);
		if (re == null)
			return l;
		else
			return re;
	}

	

}
